package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.BannerQuery;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/banner")
public class BannerController {
    @Autowired
   private IBannerService bannerService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;


    /**
     * 列表查询
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") BannerQuery qo){
        Page<Banner> page = bannerService.query(qo);
        model.addAttribute("page", page);
        return "banner/list";
    }


    @GetMapping("/getAllType")
    @ResponseBody
    public Object getAllType(){
        HashMap<Object, Object> hashMap = new HashMap<>();

        //查询所有游记
        List<Strategy> strategies = strategyService.findAll();

        //查询所有攻略
        List<Travel> travels = travelService.findAll();

        hashMap.put("sts",strategies );
        hashMap.put("ts", travels);

       return JsonResult.success(hashMap);
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Banner banner){
        if (StringUtils.hasLength(banner.getId())){
            bannerService.update(banner);
        }else {
            bannerService.insert(banner);
        }
        return JsonResult.success();
    }
}
